﻿/* 
 * yaku
 * http://code.google.com/p/yaku/
 * 
 * Copyright 2011, Bryan McKelvey
 * Licensed under the MIT license
 * http://www.opensource.org/licenses/mit-license.php
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using yaku;

namespace yaku
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Localization.SetCulture();
            InitializeComponent();
            Dictionaries.LoadEdict(ref SearchTextBox, ref ResultsDataGrid, ref StatusBarText);
            MatchTypeComboBox.SelectedIndex = (int)Dictionaries.Edict.Type;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void LanguageButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            Localization.SetCulture(rb.Tag.ToString());
        }

        private void Grid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                Close();
            }
        }

        private void SearchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Dictionaries.Edict.Search();
            }
        }

        private void DictionaryButton_Click(object sender, RoutedEventArgs e)
        {
            Dictionaries.Edict.ManualLoad();
        }

        private void KanjiButton_Click(object sender, RoutedEventArgs e)
        {
            Dictionaries.LoadKanji();
            KanjiWindow kanjiWin = new KanjiWindow(ref SearchTextBox);
            kanjiWin.Show();
        }

        private void MatchTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Dictionaries.Edict is Edict)
            {
                try
                {
                    Dictionaries.Edict.Type = (Edict.SearchType)(((ComboBox)sender).SelectedIndex);
                    SearchTextBox.Focus();
                }
                catch (Exception p)
                {
                    MessageBox.Show(p.Message);
                }
            }
        }
    }
}
